import Vue from 'vue'
import '@/utils/version'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import '@/utils/directive' // 自定义指令
import animated from 'animate.css'
import vueFragment from 'vue-fragment'
Vue.use(vueFragment.Plugin)
import Element from 'element-ui'
import './styles/element-variables.scss'

import '@/styles/index.scss' // global css
import { Message } from 'element-ui'

import App from './App'
import store from './store'
import router from './router'

// import './icons' // icon
import './permission' // permission control
import * as filters from './filters' // global filters

Vue.use(animated)

import onlyNumber from '@/directive/el-input/index'
Vue.use(onlyNumber)

import elDragDialog from '@/directive/el-drag-dialog/index'
Vue.use(elDragDialog)

import qupComponent from 'qup-component'
Vue.use(qupComponent)

// 话务条服务
import callProxyService from '@/utils/callProxyService'
import callProxyService2 from '@/utils/callProxyService2'
Vue.use(callProxyService)
Vue.use(callProxyService2)

import Button from '@/components/Button'
Vue.component('Button', Button)
import StaffSearch from '@/components/StaffSearch'
Vue.component('StaffSearch', StaffSearch)

// 全局导入 解决循环依赖
import createOrder from '@/views/workOrder/createOrder'
Vue.component('CreateOrder', createOrder)
import CallOut from '@/views/callBarPage/callOut/export'
Vue.component('CallOut', CallOut)

// 日志
// import '@/utils/logToFile'
/**
 * If you don't want to use mock-server
 * you want to use mockjs for request interception
 * you can execute:
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */

// import { mockXHR } from '../mock'
// mockXHR()
// if (process.env.NODE_ENV === 'development') {
//   require('./mock')
// }

// 刷新当标签页面
Vue.prototype.$refresh = function() {
  this.$store.dispatch('tagsView/delCachedView', this.$route).then(() => {
    const { fullPath } = this.$route
    this.$nextTick(() => {
      this.$router.replace({
        path: '/redirect' + fullPath
      })
    })
  })
}

Vue.prototype.singleJudge = (array, warnTxt, single = false) => {
  if (array.length < 1) {
    Message.warning(`请选择【${warnTxt}】项`)
    return false
  }
  if (single && array.length > 1) {
    Message.warning(`该操作仅可单选`)
    return false
  }
  return true
}

Vue.use(Element, {
  size: Cookies.get('size') || 'mini' // set element-ui default size
})

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.directive('focus', {
  inserted: function(el) {
    // 聚焦元素
    el.querySelector('input').focus()
  }
})

Vue.config.productionTip = false
// 判断平台

const host = location.host
Object.defineProperty(Vue.prototype, '$isUac', { value: /^uac/.test(host) })
Object.defineProperty(Vue.prototype, '$isCC', { value: /^cc/.test(host) })
Object.defineProperty(Vue.prototype, '$isPanda', { value: /^panda/.test(host) })
Object.defineProperty(Vue.prototype, '$isUuxt', { value: /^uuxt/.test(host) })

// workOrder
// Vue.use(window.workOrder.workOrder.default)

// 处理keep-alive
Vue.mixin({
  beforeCreate() {
    // if (this.$vnode && this.$vnode.componentOptions && this.$vnode.componentOptions.tag === 'keep-alive') {
    //   // Object.defineProperty(Vue, '$keepAlive', { value: this })
    //   this.$store.commit('tagsView/UPDATE_KEEP_ALIVE', this)
    // }
  },
  created() {
    if (this.$options.name === 'keep-alive') {
      const delComponent = (key) => {
        const componentInstance = this.cache[key].componentInstance
        delete this.cache[key]
        for (let i = 0; i < this.keys.length; i++) {
          if (this.keys[i] === key) {
            this.keys.splice(i, 1)
            break
          }
        }
        setTimeout(() => {
          if (componentInstance) {
            componentInstance.$destroy()
          }
        }, 1000)
      }
      this.ccDelCache = (view, flag) => {
        const cache = this.cache || {}
        if (flag === undefined) {
          view = [view]
        }
        view = view.map(e => e.fullPath)
        for (const key of Object.keys(cache)) {
          if (view.includes(key) && flag === undefined) {
            delComponent(key)
            break
          } else if (!view.includes(key) && flag === 'other') {
            delComponent(key)
            continue
          }
        }
      }
      this.$store.commit('tagsView/UPDATE_KEEP_ALIVE', this.ccDelCache)
    }
  }
})
import common from './common'
Vue.use(common, { store, router, duration: 2000 })
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})

